package bpm.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;

/**
 * 
 * @author ZhaoWangLong Oct 26, 2011
 *
 */
public class UniteHttpRequestUtil {
	public static boolean request=false;
	public static String getStream(String[] param, String[] values, String urlAddress){
		URL url = null;
		HttpURLConnection conn = null;
		OutputStream os = null;
		InputStream is =null;
		OutputStream ops=null;
		try {
			url=new URL(urlAddress);
			conn=(HttpURLConnection)url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			os = conn.getOutputStream();
			StringBuffer body=null;
			if(param!=null){
				body=new StringBuffer();
				for (int i = 0; i < param.length; i++) {
					if(i!=param.length-1){
						body.append(param[i]).append("=").append(values[i])
						.append("&");
					}else{
						body.append(param[i]).append("=").append(values[i]);
					}
				}
			}
			if(body!=null){
				os.write(body.toString().getBytes());
				is = conn.getInputStream();
				String path=ConstantRepository.get("bpm.tmpdir")+"/unite";
				File filePath=new File(path);
				if(!filePath.exists())
					filePath.mkdir();
				String fileName=path+"/unite.xml";
				File f=new File(fileName);
				if(!f.exists()){
					f.createNewFile();
				}
				ops=new FileOutputStream(f);
				byte[] buff = new byte[10 * 1024];
				int readLength = 0;
				while((readLength = is.read(buff)) != -1){
					ops.write(buff, 0, readLength);
				}
				return fileName;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			close(ops,os,is,conn);
		}
	}
	public static void close(Object ... obj ){
		for (int i = 0; i < obj.length; i++) {
			if(obj[i] instanceof OutputStream){
				OutputStream os=(OutputStream)obj[i];
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(obj[i]instanceof InputStream){
				InputStream is=(InputStream)obj[i];
				try {
					is.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(obj[i]instanceof Connection){
				Connection conn=(Connection)obj[i];
				try {
					conn.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	public static void deleteFile(String fileName){
		File f=new File(fileName);
		if(f.exists()){
			f.delete();
		}
	}
	public static void writeFile(byte[] str){
		OutputStream ops=null;
		try {
			ops=new FileOutputStream("D:/bpm/bpm/tmp/aa.txt",true);
			ops.write(str, 0, str.length);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			close(ops);
		}
	}
}
